home *** CD-ROM | disk | FTP | other *** search
/ Tech Arsenal 1 / Tech Arsenal (Arsenal Computer).ISO / tek-01 / strlibs.zip / SUBSTR.ASM < prev    next >
Assembly Source File  |  1993-01-04  |  2KB  |  84 lines

  1. ;       Static Name Aliases
  2. ;
  3.         TITLE   substr
  4. ;       NAME    substr.C
  5.  
  6. ;   char *substr(destination, source, offset, length)
  7. ;
  8. ;   copies length bytes from source+offset to destination, stopping
  9. ;   early if a NUL is encountered.  The difference between this and
  10. ;
  11. ;   strncpy(destination, source+offset, length)
  12. ;
  13. ;   is that if the offset is negative, it has the same effect as 0,
  14. ;   and if it exceeds strlen(source), it has the same effect as
  15. ;   strlen(source).
  16. ;
  17. ;   After the substring of source is moved to destination, a NUL byte
  18. ;   is moved to terminate the string, and the result is a pointer to
  19. ;   this NUL byte, ready to have new stuff stuck on the end.
  20.  
  21.         .287
  22. _TEXT   SEGMENT  BYTE PUBLIC 'CODE'
  23. _TEXT   ENDS
  24. _DATA   SEGMENT  WORD PUBLIC 'DATA'
  25. _DATA   ENDS
  26. CONST   SEGMENT  WORD PUBLIC 'CONST'
  27. CONST   ENDS
  28. _BSS    SEGMENT  WORD PUBLIC 'BSS'
  29. _BSS    ENDS
  30. DGROUP  GROUP   CONST,  _BSS,   _DATA
  31.         ASSUME  CS: _TEXT, DS: DGROUP, SS: DGROUP, ES: DGROUP
  32. _TEXT      SEGMENT
  33.  
  34.         PUBLIC  _substr
  35. _substr PROC NEAR
  36.         push    bp
  37.         mov     bp,sp
  38.         push    di
  39.         push    si
  40.         mov     di,[bp+4]       ;dst
  41.         mov     si,[bp+6]       ;src
  42.  
  43. ;       dst = 4
  44. ;       register di = dst
  45. ;       src = 6
  46. ;       register si = src
  47. ;       off = 8
  48. ;       len = 10
  49.  
  50.         mov     cx,[bp+8]               ;off
  51.         cmp     cx,0                    ;off
  52.         jge     $WC14
  53.         xor     cx,cx
  54. $WC14:
  55.         jcxz    $ck_len
  56.         inc     si
  57.         dec     cx
  58.         cmp     BYTE PTR [si-1],0
  59.         jne     $WC14
  60. $L20004:
  61.         mov     BYTE PTR [di],0
  62.         mov     ax,di
  63.         jmp     SHORT $EX13
  64. $ck_len:
  65.         mov     cx,word ptr [bp+10]
  66. $WB15:
  67.         jcxz    $L20004
  68.         dec     cx                  ;len
  69.         lodsb
  70.         stosb
  71.         or      al,al
  72.         jne     $WB15
  73.         lea     ax,[di-1]
  74. $EX13:
  75.         pop     si
  76.         pop     di
  77.         mov     sp,bp
  78.         pop     bp
  79.         ret
  80.  
  81. _substr ENDP
  82. _TEXT   ENDS
  83. END
  84.